package com.teamone.mobilewatch;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.teamone.mobilewatch.accessor.webservice.PatientWatchDataAccessorImpl;

/**
 * Admin activity - back-end administration
 */
public class AdminActivity extends Activity {

	private static final String TAG = "AdminActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin);

		// checks webservice if simulator is running
		// if yes - set toggle ON
		new SimulatorSwitchTask().execute(SimulatorSwitchTask.CHECK_STATE);

		// add handler to turn on/off simulator
		ToggleButton tglSimulatorToggle = (ToggleButton) findViewById(R.id.tglSimulatorToggle);
		tglSimulatorToggle.setOnClickListener(new ButtonHandler());

	}

	/**
	 * Shows progress bar 
	 */
	private void showProgressBar() {
		ToggleButton tglSimulatorToggle = (ToggleButton) findViewById(R.id.tglSimulatorToggle);
		tglSimulatorToggle.setFocusable(false);
		
		ProgressBar prbResonseView = (ProgressBar) findViewById(R.id.pbrResponse);
		prbResonseView.setVisibility(View.VISIBLE);
		TextView txtResponseStateView = (TextView) findViewById(R.id.txtResponseState);
		txtResponseStateView.setVisibility(View.VISIBLE);
	}
	
	/**
	 * Hides progress bar and displays error message if any 
	 * @param err - error message
	 */
	private void hideProgressBar(String err) {
		ProgressBar prbResonseView = (ProgressBar) findViewById(R.id.pbrResponse);
		prbResonseView.setVisibility(View.GONE);
		TextView txtResponseStateView = (TextView) findViewById(R.id.txtResponseState);
		if (err == null) {
			txtResponseStateView.setVisibility(View.GONE);
		} else {
			txtResponseStateView.setText(err);
		}
		
		ToggleButton tglSimulatorToggle = (ToggleButton) findViewById(R.id.tglSimulatorToggle);
		tglSimulatorToggle.setFocusable(true);
	}

	/**
	 * Sets simulator toggle button on/off
	 * @param checked - value 
	 */
	private void setSimulatorToggleState(boolean checked) {
		ToggleButton tglSimulatorToggle = (ToggleButton) findViewById(R.id.tglSimulatorToggle);
		tglSimulatorToggle.setFocusable(true);
		tglSimulatorToggle.setChecked(checked);
	}

	/**
	 * Asynchronous task to start/stop simulator on webservice backend or check if it is running 
	 */
	private class SimulatorSwitchTask extends AsyncTask<Integer, Void, Void> {
		public static final int START_AND_CHECK_STATE = 1;
		public static final int STOP_AND_CHECK_STATE = 0;
		public static final int CHECK_STATE = -1;
		private static final String TAG = "SimulatorSwitchTask";
		boolean response = false;
		String err = null;

		protected void onPreExecute() {
			showProgressBar();
		}

		// automatically done on worker thread (separate from UI thread)
		protected Void doInBackground(final Integer... args) {
			try {
				// args 0 - stop, 1 - start, other numbers - check if running
				
				Log.d(TAG, "args[0]=" + args[0]);
				if (args[0]==1) {
					// start simulator
					response = PatientWatchDataAccessorImpl.getInstance()
							.startSimulator(args[1]);
					Log.d(TAG, "starting simulator");
				} else if (args[0]==0){
					// stop simulator
					response = PatientWatchDataAccessorImpl.getInstance()
							.stopSimulator();
					Log.d(TAG, "stopping simulator");
				}
				response = PatientWatchDataAccessorImpl.getInstance()
						.isSimulatorRunning();
				Log.d(TAG, "checking if simulator is running ["+ response+"]");
			} catch (Exception e) {
				err = e.getMessage();
				Log.d(TAG, err);
			}
			return null;
		}

		// can use UI thread here
		protected void onPostExecute(final Void unused) {
			setSimulatorToggleState(response);
			hideProgressBar(err);
		}

	}

	/**
	 * 
	 * Class to handle button click events
	 * 
	 */
	class ButtonHandler implements OnClickListener {

		@Override
		public void onClick(View v) {

			ToggleButton tglSimulatorToggle = (ToggleButton) findViewById(R.id.tglSimulatorToggle);
			if (tglSimulatorToggle.isChecked()) {
				EditText edtWebServerSimulatorPeriod = (EditText) findViewById(R.id.edtWebServerSimulatorPeriod);
				//TODO: parse from textbox
				new SimulatorSwitchTask().execute(SimulatorSwitchTask.START_AND_CHECK_STATE, new Integer(30)); 
			} else {
				new SimulatorSwitchTask().execute(SimulatorSwitchTask.STOP_AND_CHECK_STATE); 
			}
		}

	}

}
